//
//  UILabel+function.m
//  KCDriver
//
//  Created by kcwl_jxw on 2020/9/15.
//  Copyright © 2020 bjkcwl. All rights reserved.
//

#import "UILabel+function.h"

@implementation UILabel (function)

+ (UILabel *)labelWithFont:(UIFont *)font textColor:(UIColor *)textColor labelText:(NSString *)text {
    return [UILabel labelWithFont:font textColor:textColor labelBackgroundColor:[UIColor clearColor] numberOfLines:1 textAlignment:NSTextAlignmentLeft labelText:text];
}

+ (UILabel *)labelWithFont:(UIFont *)font textColor:(UIColor *)textColor labelBackgroundColor:(UIColor *)backgroundColor numberOfLines:(NSInteger)lines textAlignment:(NSTextAlignment)textAlignment labelText:(NSString *)text {
    UILabel *label = [[UILabel alloc] init];
    label.font = font;
    label.text = text ? text : @"";
    label.textColor = textColor ? textColor : UIColor.blackColor;
    label.backgroundColor = backgroundColor ? backgroundColor : [UIColor whiteColor];
    label.numberOfLines = lines;
    label.textAlignment = textAlignment;
    return label;
}

+(UILabel *)labelWithFont:(UIFont *)font textColor:(UIColor *)textColor numberOfLines:(NSInteger)lines textAlignment:(NSTextAlignment)textAlignment {
    
    UILabel *label = [[UILabel alloc] init];
    label.font = font;
    label.textColor = textColor;
    label.numberOfLines = lines;
    label.textAlignment = textAlignment;
    return label;
}


- (void)setText:(NSString*)text lineSpacing:(CGFloat)lineSpacing {
    if (!text || lineSpacing < 0.01) {
        self.text = text;
        return;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];//设置行间距
    [paragraphStyle setLineBreakMode:self.lineBreakMode];
    [paragraphStyle setAlignment:self.textAlignment];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    self.attributedText = attributedString;
}

- (void)conversionCharacterInterval:(NSInteger)maxInteger current:(NSString *)currentString {
    CGRect rect = [[currentString substringToIndex:1] boundingRectWithSize:CGSizeMake(200,self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: self.font} context:nil];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:currentString];
    [attrString addAttribute:NSKernAttributeName value:@(((maxInteger - currentString.length) * rect.size.width)/(currentString.length - 1)) range:NSMakeRange(0, currentString.length)];
    self.attributedText = attrString;
}




@end
